package org.jvending.provisioning.deliverable;

import org.jvending.provisioning.descriptor.media.MediaType;

import javax.provisioning.Deliverable;
import javax.xml.bind.JAXBException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.Marshaller;
import java.util.logging.Logger;
import java.util.logging.Level;
import java.util.*;
import java.net.URL;
import java.net.URI;
import java.io.*;

public class OtaDescriptorFile extends BaseDescriptorFile {

    private static Logger logger = Logger.getLogger("OtaDescriptorFile");

    private final URL url;

    private final URI uri;

    private final String mimeType;

    private Map<String, String> properties = new HashMap<String, String>();

    private MediaType mediaType;

    public OtaDescriptorFile(URL url, URI uri, String mimeType, List<Deliverable> contentFiles) {
        super(url, uri, mimeType, contentFiles);
        this.url = url;
        this.uri = uri;
        this.mimeType = mimeType;
        loadProperties();
    }

    public URI getURI() {
        return uri;
    }

    public String getAppProperty(String name) {
        return properties.get(name);
    }

    public void setAppProperty(String name, String value) {
        properties.put(name, value);
    }

    public MediaType getMediaType() {
        return mediaType;
    }

    public final void writeContents(OutputStream os) throws IOException {
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance("org.jvending.provisioning.descriptor.media");
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.marshal(mediaType, os);
        } catch (JAXBException e) {
            throw new IOException(e.getMessage());
        }
    }

    private void loadProperties() {

        InputStream is = null;

        try {
            is = getURL().openStream();
            if (is == null) throw new IOException("JV-1251-004: URL Value is null");
            try {
                JAXBContext jaxbContext = JAXBContext.newInstance("org.jvending.provisioning.descriptor.media");
                Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
                unmarshaller.setValidating(true);
                mediaType = (MediaType) unmarshaller.unmarshal(is);
            } catch (JAXBException e) {
                e.printStackTrace();
                return;
            }

        } catch (IOException e) {
            logger.log(Level.INFO, "JV-1251-005:Could not load properties: URL = " + url +
                    ", Mime-Type = " + mimeType, e);
            return;
        } finally {
            try {
                if (is != null) is.close();
            } catch (IOException e) {
                logger.info("JV-1251-006: Could not close connection.");
            }
        }
        /*
        mediaType.
        ProductType productType = (ProductType) mediaType.getProduct();
        List<MediaObjectType> mediaObject = productType.getMediaObject();
        MetaType meta = productType.getMeta();
        properties.put(OtaProductMetaProperty.DELETE_NOTIFY_URI.getTagName(), meta.getDeleteNotifyURI());
        properties.put(OtaProductMetaProperty.DESCRIPTION.getTagName(), meta.getDescription());
        properties.put(OtaProductMetaProperty.DOWNLOAD_NOTIFY_URI.getTagName(), meta.getDownloadNotifyURI());
        properties.put(OtaProductMetaProperty.INFO_URL.getTagName(), meta.getInfoURL());
        properties.put(OtaProductMetaProperty.INSTALL_NOTIFY_URI.getTagName(), meta.getInstallNotifyURI());
        properties.put(OtaProductMetaProperty.NAME.getTagName(), meta.getName());
        */
    }
}
